<?php
	//echo Yii::app()->request->getParam("departemen","dep kosong");
	$departments = MDepartment::model()->findAll("is_active = TRUE order by id");
	$department_id = $department_id == "" ? $departments[0]->id : $department_id; 	
	$department_id = Yii::app()->request->getParam("department_id",$department_id);	
?>

<script>
	//var departments = <?php //echo $departments?>;
	//var department_id = "<?php //echo $department_id ?>";	
	
	function reloadPage(){		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");
	}
	
	function newData(){
		var url="index.php?r=akademik/master/jam_pelajaran/form&op=add&department_id=" + $("#department_id").val();
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = false,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function closeTest(){
		
	}
	
	function editRow(id){		
		var url="index.php?r=akademik/master/jam_pelajaran/form&op=edit&id=" + id;
		var winName = "Form Process";
		var specs ="menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0";
		var width=450,height=300;
		var replace = true,onCenter = true;
		var w = openWindow(winName,url,width,height,onCenter,specs,replace);
		w.focus();
		var timer = setInterval(function() {   
			if(w.closed) {  
				clearInterval(timer);  
				reloadPage();
			}  
		}, 1000);
	}
	
	function deleteRow(id){
		if (confirm("Delete data ?")){			
			window.parent.showLoader();			
			var data = $("#tblData").jqGrid("getRowData",id);
			var editUrl = $("#tblData").jqGrid("getGridParam","editUrl");			
			$.ajax({	
				url : editUrl,
				type:"GET",
				async:true,
				data: {op: "del",id:data.id},
				success: function(data) {
					if(data.actionStatus == 1){
						alert(data.actionMessage);
					}						
				},
				complete:function(){
					window.parent.hideLoader();
				}
			});			
			$("#tblData").jqGrid("delRowData",id);
		}
	}
	
	$(function(){
		var department_id =$("#department_id").val();		
		var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
		$("#tblData").jqGrid({
			url:'index.php?r=akademik/master/jam_pelajaran/detailList',
			editUrl:'index.php?r=akademik/master/jam_pelajaran/edit',
			postData:{"department_id":department_id},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','JamPelajaran','Status','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 40,hidden:false},
				{name: 'name', index: 'name', width: 40,search:true},
				{name: 'is_active', index: 'is_active', width: 30, align: 'center', formatter: 'checkbox'},
				{name: 'act', index: 'act', width: 40, align: 'center'},
				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'JamPelajaran',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit =   "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Actions-Edit-icon.png' onclick='editRow(\"" + cl + "\")' />"; 
					btnDelete = "<img title='delete' class='imgBtn' style='margin-left:5px;height:20px;width:20px;' src='images/action/Actions-Delete-icon.png' onclick='deleteRow(\"" + cl + "\")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit+btnDelete}) 
				} 
			}
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["name"]);
	});
</script>

<div style="">
	<script>
		$(function(){
			$("#department_id").change(function(){
				$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid");
			});
		});	
	</script>
	
	<div>
		<?php
			//$department_id = Yii::app()->request->getParam("department_id","KB");
			$rsDep = MDepartment::model()->findAll(array("condition"=>"is_active = TRUE","order"=>"id"));
			$arrOptions=array();
			foreach($rsDep as $rw){
				$selected = $department_id == $rw->id ? true : false;
				$arrOptions[] = array("label"=>$rw->name,"value"=>$rw->id,"selected"=>$selected);
			}
			$this->widget('application.components.widgets.SelectField', 
				array("name"=>"department_id",
					"value"=>"",
					"htmlOptions"=>"",
					"label"=>array(
						"value"=>"Department",
						"htmlOptions"=>"class ='mandatory' style='display:inline-block;width:100px'"
					),
					"options"=>$arrOptions,
				)
			);
			
		?>		
	</div>    
	 
	<div style="float:rigth">
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => Yii::t("menu","Segarkan"),
						'icon' => "images/action/Action-Refresh-icon.png",
						'callback'=>"reloadPage"
					),					
					array(
						'id' => "btnNew",
						'title' => Yii::t("menu","Baru"),
						'icon' => "images/action/Actions-document-new-icon.png",
						'callback'=>"newData"
					),
					array(
						'id' => "btnNew",
						'title' => Yii::t("menu","Cetak"),
						'icon' => "images/action/Actions-document-print-icon2.png",
						'callback'=>"printData"
					),					
					
				)
			)
		);
	?>
	</div>	
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
</div>